%this program fits the chunked data to the expo function: Aj*exp(Bj*x)+Cj
%which is a differnt form of the equation A(1-exp(-t/tau)+B. Aj=-A (lastpt-firstpt),
%B=-1/tau  (random guess- alginate has tau~=3-6), C=A+B (~=last point of curve)
%as of 8/3/04 this is the best version of the code. 
%updated 2018.7.12 PVT (no longer import data, but recieves data through
%input variable.  More updates coming.

function [out, out2, out3, RMSError,Rsquared]=regression(name);

%cd..; %%don't know
%cd steps
%name = '3.txt';


% X=importdata(name);
X = name;
strain=mean(X(:,3));    %pass correct



limit1=X(end,2)+X(1,2);
limit2=-.1;
limit3=X(end,2);

l=X(1,2);

xd=X(:,1)-min(X(:,1));
yd=X(:,2);

x=linspace(xd(1),1*xd(end),50); %generates time axis to be one times longer then the data set -20 points

figure(1); hold on;
fobj=fittype('A*exp(B*x)+C');

try
[f1,goodness]=fit(xd,yd,fobj,'startpoint',[limit1,limit2,limit3]); % need to figure out the limits and have them as inputs
catch err

X(:,2)=X(:,2)-max(X(:,2))-0.5;
limit1=X(end,2)+X(1,2);
limit2=-.1;
limit3=X(end,2);
l=X(1,2);

xd=X(:,1)-min(X(:,1));

[f1,goodness]=fit(xd,yd,fobj,'startpoint',[limit1,limit2,limit3]);
end

coeff=coeffvalues(f1) ;  %obtains the coeff values from cfit used in fit
                        %[A,B,C]
NRMSE=goodness.rmse;                        
R2=goodness.rsquare;
NRMSE=NRMSE/(max(yd)-min(yd));
%f2=fit(xd,yd,'exp1')/
%plot(xd,yd,'o',x,f1(x),'rx-');  %JASON- In order to do subplots you are going to have to create them in you script. This function only runs one step at a time. I can pass out the xd,yd,x,fl and you can do the plot





out=[coeff(1,1:3),strain];
out2=[xd,yd];
out3=[x',f1(x)];
RMSError=[NRMSE];
Rsquared=[R2];

%StartPoint    - Vector to be used as the starting point in FIT
 %                     [{[]} | vector of length the number of coefficients]
 
 
 %goodness is a canned stats option. it is a structure and can be accessed
 %by goodness.rsquare
 
 %Remarks
%For rationals and Weibull library models, the coefficient starting values are randomly selected in the range [0,1]. Therefore, if you perform multiple fits to a data set using the same equation, you might get different coefficient results due to different starting values. To avoid this situation, you should pass in a vector of starting values each time you fit, or define a specific state for the random number generator, rand or randn, before fitting.
%For all other library models, optimal starting points are automatically calculated. These values depend on the data, and are based on model-specific heuristics.
%http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/fit.html

%Weibull Distribution
%The Weibull distribution is widely used in reliability and life (failure rate) data analysis. The toolbox provides the two-parameter Weibull distribution 
%where a is the scale parameter and b is the shape parameter. Note that there is also a three-parameter Weibull distribution with x replaced by x - c where c is the location parameter. Additionally, there is a one-parameter Weibull distribution where the shape parameter is fixed and only the scale parameter is fitted. To use these distributions, you must create a custom equation.
%Note that the Curve Fitting Toolbox does not fit Weibull probability distributions to a sample of data. Instead, it fits curves to response and predictor data such that the curve has the same shape as a Weibull distribution.


%http://www.mathworks.com/access/helpdesk/help/toolbox/curvefit/


%StartPoint
 %Vector of coefficient starting values. The coefficients are specified by the input argument ftype for fit.
 %The default value of StartPoint is an empty vector. If the default value is passed to the fit function, 
 %then starting points for some library models are determined heuristically. For other models, the values are selected randomly on the interval (0,1). 